
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'match/match/index' + location.search,
                    add_url: 'match/match/add',
                    edit_url: 'match/match/edit',
                    //del_url: 'match/match/del',
                    multi_url: 'match/match/multi',
                    //import_url: 'match/match/import',
                    table: 'match',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'm_id',
                sortName: 'm_id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'm_id', title: __('M_id'),visible:false},
                        {field: 'merchant_id', title: __('Merchant_id'),visible:false},
                        {field: 'merchant.company_name', title: __('Merchant.company_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 's_id', title: __('S_id'),visible:false},
                        {field: 'store.store_name', title: __('Store.store_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'provinceName', title: __('Province'),operate:false},
                        {field: 'cityName', title: __('City'),operate:false},
                        {field: 'areaName', title: __('Area'),operate:false},
                        {field: 'address', title: __('Address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'lng', title: __('Lng'), operate:'BETWEEN'},
                        {field: 'lat', title: __('Lat'), operate:'BETWEEN'},
                        {field: 'sign_start_date', title: __('Sign_start_date'),addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'sign_end_date', title: __('Sign_end_date'),addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'match_date', title: __('Match_date'),addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'people_count', title: __('People_count')},
                        {field: 'least_count', title: '最少人数'},
                        {field: 'cost', title: __('Cost'), operate:'BETWEEN'},
                        {field: 'join_count', title: __('Join_count')},
                        {field: 'order_count', title: __('Order_count')},
                        {field: 'avg_amount', title: __('Avg_amount'), operate:'BETWEEN'},
                        {field: 'condition', title: __('Condition'), table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'description', title: __('Description'),table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1')}, formatter: Table.api.formatter.normal},
                        {field: 'rounds', title: __('Rounds')},
                        {field: 'group_people_number', title: __('Group_people_number')},
                        {field: 'group_inner_numbers', title: '小组人数'},
                        {field: 'release_time', title: __('Release_time'), operate:'BETWEEN',addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'ct_date', title: __('Ct_date'),addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'up_date', title: __('Up_date'),addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'delete', title: __('Delete'),searchList: {"0":__('Delete 0'),"1":__('Delete 1')}, formatter: Table.api.formatter.toggle},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons:[
                                {
                                    name:'协议签署',
                                    extend:'data-area=["90%","90%"]',
                                    title:'协议签署',
                                    icon:'fa fa-magic',
                                    text:'协议签署',
                                    classname:'btn btn-xs btn-info btn-dialog',
                                    url:'match/agreement/index',
                                    refresh:true
                                },
                                {
                                    name:'成员管理',
                                    extend:'data-area=["90%","90%"]',
                                    title:'成员管理',
                                    text:'成员管理',
                                    icon:'fa fa-magic',
                                    classname:'btn btn-xs btn-info btn-dialog',
                                    url:'match/member/index',
                                    refresh:true
                                },
                                {
                                    name:'轮次管理',
                                    extend:'data-area=["90%","90%"]',
                                    title:'轮次管理',
                                    icon:'fa fa-magic',
                                    text:'轮次管理',
                                    classname:'btn btn-xs btn-info btn-dialog',
                                    url:'match/round/index',
                                    refresh:true
                                },
                                /*{
                                    name:'比赛管理',
                                    extend:'data-area=["90%","90%"]',
                                    title:'比赛管理',
                                    icon:'fa fa-magic',
                                    text:'比赛管理',
                                    classname:'btn btn-xs btn-info btn-dialog',
                                    url:'match/roundmember/index',
                                    refresh:true
                                },*/
                                {
                                    name:'裁判管理',
                                    extend:'data-area=["90%","90%"]',
                                    title:'裁判管理',
                                    icon:'fa fa-magic',
                                    text:'裁判管理',
                                    classname:'btn btn-xs btn-info btn-dialog',
                                    url:'match/referee/index',
                                    refresh:true
                                },
                                {
                                    name:'排行榜',
                                    extend:'data-area=["90%","90%"]',
                                    title:'排行榜',
                                    icon:'fa fa-magic',
                                    text:'排行榜',
                                    classname:'btn btn-xs btn-info btn-dialog',
                                    url:'match/ranks/index',
                                    refresh:true
                                },
                                {
                                    name:'结束',
                                    title:'结束',
                                    icon:'fa fa-magic',
                                    text:'结束',
                                    confirm: '确定执行此操作吗?',
                                    classname:'btn btn-xs btn-success btn-ajax',
                                    url:'match/match/end',
                                    visible:function(row){
                                        if (row.status==0){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    refresh:true
                                },
                                {
                                    name:'取消',
                                    title:'取消',
                                    icon:'fa fa-magic',
                                    text:'取消',
                                    confirm: '确定执行此操作吗?',
                                    classname:'btn btn-xs btn-success btn-ajax',
                                    url:'match/match/cancel',
                                    visible:function(row){
                                        if (row.status==0){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    refresh:true
                                },
                                /*{
                                    name:'生成排行',
                                    title:'生成排行',
                                    icon:'fa fa-magic',
                                    text:'生成排行',
                                    confirm: '确定执行此操作吗?',
                                    classname:'btn btn-xs btn-success btn-ajax',
                                    url:'match/match/ranks',
                                    refresh:true
                                }*/
                            ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-s_id").data("params", function (obj) {
                return {"custom[merchant_id]":$("#c-merchant_id").val()};
            });

            /**
             * 取消 selectpage 选项按钮功能
             */
            $('#c-referee').on('focus',function (){
                $('.sp_control_box').remove();
            });

            /**
             *
             */
            $('#c-referee').data("eSelect", function (data) {
                var htmlStr ='';
                $.each(Config.purview,function (key, value) {
                    htmlStr += `<input type="checkbox" name="row[purview][${data.id}][]" value="${value.value}" >${value.title}</input>`;
                });
                var html = `<div id="checkbox_${data.id}" style="width: 100%;display: flex">
                                    <div style="width: 30%;">
                                        <span style="width: 100%;">${data.ushowfiled}:</span>
                                    </div>
                                    <div style="width: 70%;">${htmlStr}</div></div>`;
                $('#checkbox_power').append(html);
            });

            $('#c-referee').data("eTagRemove", function (data) {
                $('#checkbox_'+data[0].id).remove();
            });

            Controller.api.bindevent();
        },
        edit: function () {
            $("#c-s_id").data("params", function (obj) {
                return {"custom[merchant_id]":$("#c-merchant_id").val()};
            });

            /**
             * 取消 selectpage 选项按钮功能
             */
            $('#c-referee').on('focus',function (){
                $('.sp_control_box').remove();
            });

            $('#c-referee').data("eSelect", function (data) {
                var htmlStr ='';
                $.each(Config.purview,function (key, value) {
                    htmlStr += `<input type="checkbox" name="row[purview][${data.id}][]" value="${value.value}" >${value.title}</input>`;
                });
                var html = `<div id="checkbox_${data.id}" style="width: 100%;display: flex">
                                    <div style="width: 30%;">
                                        <span style="width: 100%;">${data.ushowfiled}:</span>
                                    </div>
                                    <div style="width: 70%;">${htmlStr}</div></div>`;
                $('#checkbox_power').append(html);
            });

            $('#c-referee').data("eTagRemove", function (data) {
                if (data.length > 0){
                    $('#checkbox_'+data[0].id).remove();
                }
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
